// pages/goodsEvaluate/goodsEvaluate.js
const app = getApp();
const api = require('../../utils/api');
const constant = require('../../utils/constant');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    // 底部菜单高度
    tabbarHeight: app.globalData.tabbarHeight,
    BASEURLWWW:constant.BASEURLWWW,
    // 评论列表
    goodsEvaluateData: [],
    // 详情显示
    evaluateShow: false,
    // 头部标题
    headerTitle: '全部评价',
    // 商品评论列表分页
    goodsEvaluatePagination:{
      pageNum: 1,  //分页初始值，页号1
      pageSize: 2, //分页初始值，每页20条
      total: null
    },
    // 条件筛选
    screenData:[
      {id:0,name:'全部'},
      {id:1,name:'好评'},
      {id:2,name:'中评'},
      {id:3,name:'差评'},
      {id:4,name:'有图'},
      {id:5,name:'无图'},
    ],
    screenActive: 0,
    // 筛选条件
    screen:{
      goods_id: null,
      is_image: null,
      score: null
    },
    // 是否有数据
    noDataNoticeStatus: false,
    // flash
    flash: [],
    // 是否显示图片弹出个你
    isImgShow: false,
    // 显示第几张图
    swiperCurrent: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.goods_id){
      this.setData({
        'screen.goods_id': options.goods_id
      })
    }
    this.getGoodsEvaluateData(this.data.screen,this.data.goodsEvaluatePagination);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击搜索框
  jumpToSearch: function() {
    wx.reLaunch({
       url: '/pages/search/search'
     })
   },
   // 返回到首页
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  // 获取商品评价数据
  getGoodsEvaluateData:function(screen,pagination){
    let that = this;
    let goodsEvaluateDataArr = this.data.goodsEvaluateData;
    // 数据已经到底
    if(goodsEvaluateDataArr.length == this.data.goodsEvaluatePagination.total){
      wx.showToast({
        title: '已经没有了~',
        icon: 'none',
        duration: 1500
      })
    }else{
      api.getGoodsEvaluate(screen,pagination).then(res => {
        if(res.code == 0){
          if(res.data.data.total != 0){
            goodsEvaluateDataArr.push(...res.data.data.list);
            that.setData({
              goodsEvaluateData: goodsEvaluateDataArr,
              'goodsEvaluatePagination.total': res.data.data.total,
              noDataNoticeStatus: false
            })
          }else{
            this.setData({
              noDataNoticeStatus: true
            })
          }
        }
      })
    }
    
  },
  // 滚动到底部
  bindscrolltolower:function(){
    let pageNum = this.data.goodsEvaluatePagination.pageNum+1;
    this.setData({
      'goodsEvaluatePagination.pageNum': pageNum
    })
    this.getGoodsEvaluateData(this.data.screen,this.data.goodsEvaluatePagination);
  },
  // 条件筛选
  screen:function(e){
    let id = e.currentTarget.dataset.id;
    let screen = this.data.screen;
    let screenActive = this.data.screenActive;
    // 全部
    if(id == 0){
      screen.is_image = null;
      screen.score = null;
    }
    // 好评
    if(id == 1){
      screen.is_image = null;
      screen.score = 5;
    }
    // 中评
    if(id == 2){
      screen.is_image = null;
      screen.score = 3;
    }
    // 差评
    if(id == 3){
      screen.is_image = null;
      screen.score = 1;
    }
    // 有图
    if(id == 4){
      screen.is_image = 1;
      screen.score = null;
    }
    // 无图
    if(id == 5){
      screen.is_image = 0;
      screen.score = null;
    }
    if(screenActive != id){
      this.setData({
        screen,
        screenActive: id,
        goodsEvaluateData: [],
        'goodsEvaluatePagination.total': null,
        'goodsEvaluatePagination.pageNum': 1
      })
      this.getGoodsEvaluateData(this.data.screen,this.data.goodsEvaluatePagination);
    } 
  },
  // 查看图片
  showImg:function(e){
    let imgArr = e.currentTarget.dataset.img;
    let imgIndex = e.currentTarget.dataset.imgindex;
    this.setData({
      flash: imgArr,
      isImgShow: true,
      swiperCurrent: imgIndex
    })
  },
  // 关闭图片
  closeShowImg:function(){
    this.setData({
      isImgShow: false
    })
  }
})